//
//  KeyTableViewCell.m
//  zhijian
//
//  Created by lcb on 2019/4/5.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "KeyTableViewCell.h"
@interface KeyTableViewCell ()
@property (strong, nonatomic) UILabel *lblKey;
@property (strong, nonatomic) UIView *keyView;
@property (strong, nonatomic) UIView *lineView;
@end
@implementation KeyTableViewCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setup];
        
    }
    return self;
}
-(void)setup{
    _lblKey = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH-20, 20)];
    _lblKey.font = [UIFont systemFontOfSize:18];
    _lblKey.textColor=UIColorOfHex(0x333333);
    _lblKey.text =@"关键字范围";
    [self addSubview:_lblKey];
    //
    self.keyView = [[UIView alloc] initWithFrame:CGRectMake(20, 40, SCREEN_WIDTH-40, 80)];
    self.keyView.backgroundColor=[UIColor clearColor];
    [self addSubview:self.keyView];
    [self reloadkeyView:0];
    //
    self.lineView = [UIView new];
    self.lineView.backgroundColor=UIColorOfHex(0xe3e3e3);
    [self.contentView addSubview:self.lineView];
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.lineView.frame=CGRectMake(10, self.bounds.size.height-1, SCREEN_WIDTH-20, 1);
}
- (void)btnAction:(UIButton *)sender{
    NSLog(@"this is current selected indexs: %d",sender.tag);
    if (_keySelectedBlock) {
        _keySelectedBlock(sender.tag);
    }
    [self reloadkeyView:sender.tag];
}
-(void)reloadkeyView:(NSInteger)tag
{
    for (UIView *subView in [self.keyView subviews]) {
        [subView removeFromSuperview];
    }
    NSArray *keyArray = @[@"中文名",@"英文名",@"CAS号",@"CI号",@"序号",];
    NSInteger column = 3;  //列数
    NSInteger buttonCount = keyArray.count;//按钮个数
    //按钮的宽高
    CGFloat buttonW = self.keyView.frame.size.width / column;
    CGFloat buttonH = 40;
    for (int i = 0; i < buttonCount; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(((i % column) * buttonW), ((i / column) * buttonH), buttonW - 10, buttonH - 10);
        btn.tag = i;
        btn.titleLabel.font =[UIFont systemFontOfSize:14];
        [btn setTitle:keyArray[i] forState:UIControlStateNormal];
        [btn setTitleColor:UIColorOfHex(0x000000) forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"key_nor"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"key_sel"] forState:UIControlStateSelected];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        //监听按钮点击
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.keyView addSubview:btn];
        if (btn.tag == tag) {
            btn.selected = YES;
        }else{
            btn.selected = NO;
        }
    }
    
}
@end
